package com.zzg.srb.sms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.zzg.srb.common.exception.Assert;
import com.zzg.srb.common.exception.BusinessException;
import com.zzg.srb.common.result.ResponseEnum;
import com.zzg.srb.sms.service.SmsService;
import com.zzg.srb.sms.utils.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.Map;

/**
 * @author zhengzg
 * @Date 2022-06-02
 * @Version 1.0
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Override
    public void send(String phone, String templateCode, Map<String, Object> params) {
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            client = SmsServiceImpl.createClient(SmsProperties.ACCESSKEYID, SmsProperties.ACCESSKEYSECRET);
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject jsonObject = new JSONObject(params);
        String s = jsonObject.toJSONString();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":"+s+"}");
        // 复制代码运行请自行打印 API 的返回值
        try {
            //通信失败
            Assert.isNull(client,ResponseEnum.ALIYUN_SMS_ERROR);
            log.error("SmsServiceImpl.send" + ResponseEnum.ALIYUN_PESPONSE_ERROR.getMessage());
            SendSmsResponse response = client.sendSms(sendSmsRequest);

            String code = response.getBody().getCode();

            Assert.notEquals("isv.BUSINESS_LIMIT_CONTROL",code,ResponseEnum.ALIYUN_SMS_LIMIT_CONTROL_ERROR);
            Assert.equals("ok",code,ResponseEnum.ALIYUN_SMS_ERROR);

        } catch (Exception e) {
            //调用错误
            log.error("短信接口调用失败", e.getMessage());
            throw new BusinessException(ResponseEnum.ALIYUN_SMS_ERROR);
        }
    }

    @Override
    public void send(String phone, String code) {
        log.info(code);
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            client = SmsServiceImpl.createClient(SmsProperties.ACCESSKEYID, SmsProperties.ACCESSKEYSECRET);
        } catch (Exception e) {
            e.printStackTrace();
        }

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":"+code+"}");
        // 复制代码运行请自行打印 API 的返回值
        try {
            Assert.notNull(client,ResponseEnum.ALIYUN_SMS_ERROR);
            SendSmsResponse response = client.sendSms(sendSmsRequest);

            String responseCode = response.getBody().getCode();
            log.info(responseCode);

            Assert.notEquals("isv.BUSINESS_LIMIT_CONTROL",responseCode,ResponseEnum.ALIYUN_SMS_LIMIT_CONTROL_ERROR);
            Assert.equals("OK",responseCode,ResponseEnum.ALIYUN_SMS_ERROR);
        } catch (Exception e) {
            //调用错误
            log.error("短信接口调用失败", e.getMessage());
            throw new BusinessException(ResponseEnum.ALIYUN_SMS_ERROR);
        }
    }

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
}
